package kr.saeildamil.androidstudty.ch17;

import kr.saeildamil.androidstudty.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Common extends Activity {
	final static int ADD = 0;
	final static int SUB = 1;
	final static int MUL = 2;
	
	EditText l_Value;
	EditText r_Value;
	EditText oper_Value;
	EditText result;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch17_common);
		l_Value = (EditText) findViewById(R.id.ch17_common_left);
		r_Value = (EditText) findViewById(R.id.ch17_common_right);
		oper_Value = (EditText) findViewById(R.id.ch17_common_oper);
		result = (EditText) findViewById(R.id.ch17_common_result);
	}
	
	public void mOnClick(View v){
		if (v.getId()==R.id.ch17_common_btn){
			Intent intent = new Intent();
			intent.putExtra("lValue", l_Value.getText().toString());
			intent.putExtra("rValue", r_Value.getText().toString());
			
			String opr = oper_Value.getText().toString().trim();
			if (opr.equals("+")){
				intent.setClass(this, AddAct.class);
				startActivityForResult(intent, ADD);
			}else if (opr.equals("-")){
				intent.setClass(this, SubAct.class);
				startActivityForResult(intent, SUB);
			}else{
				intent.setClass(this, MulAct.class);
				startActivityForResult(intent, MUL);
			}
		}
	}//end of method mOnClick
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(requestCode){
		case ADD:
			if (resultCode == RESULT_OK){
				Toast.makeText(this, "Add결과 완료", Toast.LENGTH_SHORT).show();
				result.setText(data.getIntExtra("AddResult", 0)+"");
			}else{
				Toast.makeText(this, "Add결과실패", Toast.LENGTH_SHORT).show();
			}
			break;
		case SUB:
			if (resultCode == RESULT_OK){
				Toast.makeText(this, "Sub결과 완료", Toast.LENGTH_SHORT).show();
				result.setText(data.getIntExtra("SubResult", 0)+"");
			}else{
				Toast.makeText(this, "Sub결과실패", Toast.LENGTH_SHORT).show();
			}
			break;
		case MUL:
			if (resultCode == RESULT_OK){
				Toast.makeText(this, "Mul결과 완료", Toast.LENGTH_SHORT).show();
				result.setText(data.getIntExtra("MulResult", 0)+"");
			}else{
				Toast.makeText(this, "Mul결과실패", Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}
	
}















